#'
#' @title linkET R包绘制相关性图
#' @description linkET R包绘制相关性图
#' @param df 需要做相关性的表格，
#'                 Antigen Processing and Presentation Antimicrobials
#‘ TCGA-05-4250-01                           0.2549177     0.08990811
#‘ TCGA-05-4382-01                           0.2420056     0.09561866
#‘ TCGA-05-4384-01                           0.2193338     0.07760394
#‘ TCGA-05-4389-01                           0.2720519     0.09213100
#‘ TCGA-05-4390-01                           0.2089369     0.07001091
#‘ TCGA-05-4396-01                           0.2199287     0.05841594
#‘                 BCRSignalingPathway  Chemokines Chemokine Receptors   Cytokines
#‘ TCGA-05-4250-01           0.1670376  0.08494045        -0.026708644 -0.06047277
#‘ TCGA-05-4382-01           0.2041540  0.10163717         0.005733146 -0.04945282
#‘ TCGA-05-4384-01           0.1549674 -0.01368222        -0.021272092 -0.09894723
#‘ TCGA-05-4389-01           0.1632797  0.06486740        -0.040391705 -0.07514785
#‘ TCGA-05-4390-01           0.1458482  0.03744347        -0.064204006 -0.07284104
#‘ TCGA-05-4396-01           0.1506017 -0.01384539        -0.074100672 
#' @param var 用于绘制线条相关性的变量，是df种的某一列或者几列 
#' @param type 线条方向，lower 或者 upper 
#' @export
#' @return 一个ggplot2对象
#' @author *WYK*
#'
linkET_cor_p <- \(df, var = "Score", type = "lower"){
    require(linkET)

    res <- psych::corr.test(df %>% select(any_of(var)),
        df %>% select(-any_of(var)),
        method = "spearman", adjust = "BH"
    )

    df_p <- tibble(var1 = var, var2 = colnames(res$r), r = as.numeric(res$r), p = as.numeric(res$p))
    df_p %<>%
        mutate(
            rd = cut(abs(r),
                breaks = c(-Inf, 0.1, 0.3, .5, Inf),
                labels = c("<0.1", "0.1-0.3","0.3-0.5", ">=0.5")
            ),
            pd = cut(p,
                breaks = c(-Inf, 0.01, 0.05, Inf),
                labels = c("<0.01", "0.01-0.05", ">=0.05")
            ),
            type = ifelse(r > 0 ,'Positive','Negative')
        )

    c_res <- correlate(df, method = "spearman", adjust = T, adjust_method = "BH")

    a <- qcorrplot(c_res, type = type, diag = FALSE) +
        geom_square() +
        geom_couple(aes(colour = pd, size = rd,linetype = type),
            data = df_p,
            curvature = nice_curvature()
        ) +
        scale_fill_gradientn(colours = RColorBrewer::brewer.pal(11, "RdBu"), limits = c(-1, 1)) +
        scale_size_manual(values = c(0.5, 1, 2)) +
        scale_colour_manual(values = color_pal(3)) +
        guides(
            size = guide_legend(
                title = "Spearman's r",
                override.aes = list(colour = "grey35"),
                order = 2
            ),
            colour = guide_legend(
                title = "Spearman's p",
                override.aes = list(size = 3),
                order = 1
            ),
            fill = guide_colorbar(title = "Spearman's r", order = 3),
            linetype = guide_legend(title = "Relations")
        )
    return(a)
}